//
//  DSSHProjectMainVC.m
//  DSSH
//
//  Created by lcy on 16/3/28.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "DSSHProjectMainVC.h"
#import "UIView+Round.h"
#import "ProjectBtn.h"
#import "DSSHProjectScanVC.h"
#import "DSSHProjectRockVC.h"
#import "UIView+Ext.h"
#import "CusNavButton.h"
#define CUSBUTTONHIGHT 35
@interface DSSHProjectMainVC ()<UISearchBarDelegate,BMKMapViewDelegate,BMKLocationServiceDelegate>
{
    BMKMapView *_mapView;
    BMKLocationService *_locService;
    UIImageView * _Taskview;

}
@property(nonatomic,strong)UISearchBar *searchBar;
@property(nonatomic,strong)UIView *coverView;

@end

@implementation DSSHProjectMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewAndData];
    // Do any additional setup after loading the view.
}
-(void)setupViewAndData
{
    [self setNavigationView];
   
    //添加地图部分
    _mapView = [[BMKMapView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_mapView];
    _mapView.showsUserLocation = YES;
    _mapView.delegate = self;
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [_locService startUserLocationService];
    
    //添加大头针
    BMKPointAnnotation* annotation1 = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor1;
    coor1.latitude = 39.915;
    coor1.longitude = 116.404;
    annotation1.coordinate = coor1;
    annotation1.title = @"这里是北京";
    annotation1.subtitle = @"1";
    [_mapView addAnnotation:annotation1];
    
    BMKPointAnnotation* annotation2 = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor2;
    coor2.latitude = 39.965;
    coor2.longitude = 116.404;
    annotation2.coordinate = coor1;
    annotation2.title = @"这里是北京";
    annotation2.subtitle = @"2";
    [_mapView addAnnotation:annotation2];
    
    BMKPointAnnotation* annotation3 = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor3;
    coor3.latitude = 39.905;
    coor3.longitude = 116.654;
    annotation3.coordinate = coor3;
    annotation3.title = @"这里是北京";
    annotation3.subtitle = @"3";
    [_mapView addAnnotation:annotation3];

}
-(void)setNavigationView
{
    UIView * barView = [UIView new];
    barView.frame = CGRectMake(0, 0, ZYAppWidth, 64);
    self.navigationItem.titleView = barView;
    [barView addSubview:[self createview]];
    
    ProjectBtn * settingBtn = [ProjectBtn new];
    settingBtn.frame = CGRectMake(8*WIDTH_SCALE, 18, 20*WIDTH_SCALE, 32);
    settingBtn.LabelTitle.text = @"设置";
    settingBtn.LabelTitle.font = FONT(8);
    settingBtn.image.image = [UIImage imageNamed:@"list"];
//    [settingBtn addTarget:self action:@selector(saoBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:settingBtn];

    ProjectBtn * locationBtn = [ProjectBtn new];
    locationBtn.frame = CGRectMake(ZYAppWidth-(20+20+40)*WIDTH_SCALE, 18, 20*WIDTH_SCALE, 32);
    locationBtn.LabelTitle.text = @"定位";
    locationBtn.LabelTitle.font = FONT(8);

    locationBtn.image.image = [UIImage imageNamed:@"coordinate"];
    [locationBtn addTarget:self action:@selector(locationBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:locationBtn];
    
    ProjectBtn * taskBtn = [ProjectBtn new];
    taskBtn.frame = CGRectMake(ZYAppWidth-(20+20)*WIDTH_SCALE, 18, 20*WIDTH_SCALE, 32);
    taskBtn.LabelTitle.text = @"任务";
    taskBtn.LabelTitle.font = FONT(8);

    taskBtn.image.image = [UIImage imageNamed:@"dakaizhuangtai"];
    [taskBtn addTarget:self action:@selector(taskBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:taskBtn];
}
-(UIView *)createview
{
    
    UIView *Mview = [[UIView alloc] initWithFrame:CGRectMake(50*WIDTH_SCALE, 15, ZYAppWidth-50*WIDTH_SCALE-90*WIDTH_SCALE, 30)];
    Mview.backgroundColor = [UIColor whiteColor];
    Mview.layer.cornerRadius = 5;
    
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fangdajing"]];
    [Mview addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@20);
        make.centerY.equalTo(Mview.mas_centerY);
        make.left.equalTo(Mview.mas_left).with.offset(5);
    }];
    UIButton *secarchbutton = [[UIButton alloc] init];
    [secarchbutton addTarget:self action:@selector(saoyisao) forControlEvents:UIControlEventTouchUpInside];
    [Mview addSubview:secarchbutton];
    [secarchbutton setBackgroundImage:[UIImage imageNamed:@"saoyisao"] forState:UIControlStateNormal];
    [secarchbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.centerY.equalTo(Mview.mas_centerY);
        make.width.equalTo(@20);
        make.right.equalTo(Mview.mas_right).with.offset(-5);
    }];
    
    UITextField *textfield = [[UITextField alloc] init];
    [Mview addSubview:textfield];
    textfield.font = [UIFont systemFontOfSize:13.0];
    textfield.placeholder = @"搜索相关内容";
    [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageview.mas_right).with.offset(0);
        make.centerY.equalTo(Mview.mas_centerY);
        make.height.equalTo(@30);
        make.right.equalTo(secarchbutton.mas_left).with.offset(0);
        
    }];
    
    
    
    return Mview;
    
}
#pragma 点击扫一扫
-(void)saoyisao
{
    DSSHProjectScanVC * scanVC = [[DSSHProjectScanVC alloc]init];
    [self.navigationController pushViewController:scanVC animated:YES];
}
#pragma 点击定位
-(void)locationBtnClick
{
    DSSHProjectRockVC * rockVC = [[DSSHProjectRockVC alloc]init];
    [self.navigationController pushViewController:rockVC animated:YES];
}
#pragma 点击任务
-(void)taskBtnClick
{
//    [self createSeleteview];
    CGFloat Thight = self.view.frame.size.height *0.7;
    CGFloat Twigth = self.view.frame.size.width *0.5;
    CGFloat Tx     = self.view.frame.size.width *0.5+5;
    CGFloat Ty     = 60;
    static int count = 0;
    count++;
    if (count%2==0)
    {
        //移除
        [UIView animateWithDuration:0.3 animations:^{
            _Taskview.frame = CGRectMake(Tx, Ty , Twigth,0);
        } completion:^(BOOL finished) {
            [_Taskview removeFromSuperview];
            [self.coverView removeFromSuperview];
//            _Taskview = nil;
        }];
    }else
    {
        //展开
        UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self.view addSubview:coverView];
        self.coverView = coverView;
        UIImage *image = [UIImage imageNamed:@"renwukuang"];
        
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0,image.size.width-30,0,30) resizingMode:UIImageResizingModeStretch];
        _Taskview = [[UIImageView alloc] initWithFrame:CGRectMake(Tx, Ty , Twigth,0)];
        [_Taskview setImage:image];
        _Taskview.userInteractionEnabled = YES;
        [[UIApplication sharedApplication].keyWindow addSubview:_Taskview];
        [UIView animateWithDuration:0.5 animations:^{
            _Taskview.frame = CGRectMake(Tx, Ty , Twigth, Thight);
        } completion:^(BOOL finished) {
            UIButton *buttonDown = [[UIButton alloc] init];
            buttonDown.backgroundColor = COLOR_NAVIGATE_BAR;
            [buttonDown setTitle:@"任务类型" forState:UIControlStateNormal];
            CGSize titleSize = [buttonDown.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:FONT(14)}];
            
            buttonDown.titleLabel.textAlignment = NSTextAlignmentLeft;
            buttonDown.tintColor = [UIColor whiteColor];
            buttonDown.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
            buttonDown.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width+35, 0, -titleSize.width-35);
            //            _taskbutton = buttonDown;
            [buttonDown addTarget:self action:@selector(downlistbutton) forControlEvents:UIControlEventTouchUpInside];
            [buttonDown setImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateNormal];
            [_Taskview addSubview:buttonDown];
            buttonDown.titleLabel.font = [UIFont systemFontOfSize:14.0];
            [buttonDown mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_Taskview.mas_top ).with.offset(14);
                make.centerX.equalTo(_Taskview.mas_centerX);
                make.width.mas_equalTo(_Taskview.frame.size.width -30);
                make.height.mas_equalTo(30);
            }];
            NSArray * imageArray = [[NSArray alloc]initWithObjects:@"xiangmugaikuang1",@"dengdaihuifu",@"lingqu",@"kaishishiyong", nil];
            NSArray * titleArray = [[NSArray alloc]initWithObjects:@"全部",@"待确认",@"待领取",@"执行中", nil];
            for (int i = 0; i<4; i++)
            {
                UIButton * btn = [[UIButton alloc]init];
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, -34*WIDTH_SCALE, -34*WIDTH_SCALE, 0);
                btn.imageEdgeInsets = UIEdgeInsetsMake(-26*HIGHT_SCALE, 0, 0, -34*WIDTH_SCALE);
                [btn setTitle:titleArray[i] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                btn.frame = CGRectMake(Twigth/2-40/2*WIDTH_SCALE, 70*HIGHT_SCALE+(60+20)*i*HIGHT_SCALE, 40*WIDTH_SCALE, 60*HIGHT_SCALE);
                [btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
                btn.titleLabel.textAlignment = NSTextAlignmentRight;
                btn.titleLabel.font = FONT(12);
                [_Taskview addSubview:btn];
            }
            
            UIButton *searchBtn = [UIButton new];
            [searchBtn setTitle:@"高级搜索" forState:UIControlStateNormal];
            searchBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
            searchBtn.titleLabel.font = FONT(13);
            [searchBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];;
            [searchBtn setImage:[UIImage imageNamed:@"fangdajing"] forState:UIControlStateNormal];
            [searchBtn setBackgroundColor:RGBA(252, 222, 224, 1)];
            [_Taskview addSubview:searchBtn];
            searchBtn.frame = CGRectMake(0, Thight-40, Twigth, 30);
//            [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.bottom.equalTo(_Taskview.mas_bottom ).with.offset(-10*HIGHT_SCALE);
//                make.left.equalTo(_Taskview.mas_left).with.offset(0);
//                make.width.mas_equalTo(Twigth);
//                make.height.mas_equalTo(30*HIGHT_SCALE);
//            }];
            UIButton *sendBtn = [UIButton new];
            [sendBtn setTitle:@"发布任务" forState:UIControlStateNormal];
            sendBtn.titleLabel.font = FONT(13);
            [sendBtn getRoundView:5];
            [sendBtn setBackgroundColor:RGBA(242, 80, 87, 1)];
            [_Taskview addSubview:sendBtn];
            [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(searchBtn.mas_top ).with.offset(-15*HIGHT_SCALE);
                make.centerX.equalTo(_Taskview.mas_centerX);
                make.width.mas_equalTo(90*WIDTH_SCALE);
                make.height.mas_equalTo(30*HIGHT_SCALE);
            }];
        }];

    }
}
#pragma 点击任务类型按钮
-(void)downlistbutton
{
    
}
#pragma BMKLocationSearveDelegate
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _mapView.showsUserLocation = YES;
    [_mapView updateLocationData:userLocation];
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
//    [_locService stopUserLocationService];
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        
        
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        
        newAnnotationView.annotation=annotation;
        if ([newAnnotationView.annotation.subtitle isEqualToString:@"1"])
        {
            newAnnotationView.image = [UIImage imageNamed:@"1.png"];   //把大头针换成别的图片

        }
        if ([newAnnotationView.annotation.subtitle isEqualToString:@"2"])
        {
            newAnnotationView.image = [UIImage imageNamed:@"2.png"];   //把大头针换成别的图片
            
        }
        if ([newAnnotationView.annotation.subtitle isEqualToString:@"3"])
        {
            newAnnotationView.image = [UIImage imageNamed:@"3.png"];   //把大头针换成别的图片
            
        }
        return newAnnotationView;
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
